﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Quartz;
using SMECustomerWebsite.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace SMECustomerWebsite.Jobs.SmartSMEIntegration
{
    public class ExportFreedomCustomer : JobBase, IJob
    {
        #region Properties
        public UserRecord CurrentUserRecord { get; set; }

        #endregion
        public void Execute(IJobExecutionContext context)
        {
            CurrentUserRecord = new UserRecord
            {
                UserName = "admin",
                GroupIdentifier = "1"
            };
            var listCustomer = GetDataFreedomCustomer();
            Thread.Sleep(100);
        }
        private List<CustomerFreedomRecord> GetDataFreedomCustomer()
        {
            List<CustomerFreedomRecord> resultCustomer;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                RetrieveCustomerFreedomRequest request = new RetrieveCustomerFreedomRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerFreedomResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerFreedom(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerFreedomRecord>() : customerRecords.Records;
            }
            return resultCustomer;
        }
    }
}